// pages/shop/shop-add-address.js
const app = getApp();
const util = require('../../utils/util.js');
let _this;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['请选择', '请选择', '请选择']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this = this;
    let type, index
    options.type ? type = options.type:'';
    options.index ? index = options.index:'';
    if(type){
      let pages = getCurrentPages(),
        prevPage = pages[pages.length - 2],
        editAddress = prevPage.data.myAddressList[index];

      _this.setData({
        type: type,
        index: index,
        id: editAddress.id,
        editAddress: editAddress,
        name: editAddress.userName,
        postalCode: editAddress.postalCode,
        phoneNo: editAddress.telNumber,
        region: [editAddress.provinceName, editAddress.cityName, editAddress.areaName],
        address: editAddress.detailInfo
      })
    }
    _this.setData({
      userInfo: app.globalData.xieYouUserInfo
    })
  },
  submitFun:function(e){
    if (_this.verifyMessage()){
      let data = {}, url;
      // 判断是否是修改
      _this.data.type ? url = 'addressEdit' : url ='addressAdd'
      data.id = _this.data.id;
      data.userName = _this.data.name;
      data.telNumber = _this.data.phoneNo;
      // data.userId = _this.data
      data.postalCode = _this.data.postalCode;
      data.provinceName = _this.data.region[0];
      data.cityName = _this.data.region[1];
      data.areaName = _this.data.region[2];
      data.detailInfo = _this.data.address;
      data.userCode = _this.data.userInfo.code;
      app.getMassage(data, null, url, function (res) {
        if (res.status == 'success') {
          let pages = getCurrentPages(),
            prevPage = pages[pages.length - 2];
          prevPage.getAddressList();
          if (!_this.data.buttonClicked) {
            util.buttonClicked(_this);
            wx.navigateBack({
              delta: 1
            })
          }
        }
      })
    }
  },
  // 输入信息
  massageChange: function (e) {
    var userType = e.currentTarget.dataset.type
    var massage = e.detail.value; // userLeaveMassage 留言
    switch (userType) {
      case 'name':
        _this.setData({ name: massage })
        break;
      case 'postalCode':
        _this.setData({ postalCode: massage })
        break;
      case 'phoneNo':
        _this.setData({ phoneNo: massage })
        break;
      case 'address':
        _this.setData({ address: massage })
        break;
    }
  },
  // 验证信息
  verifyMessage: function (font) {
    let name = _this.data.name,
      postalCode = _this.data.postalCode, 
      phoneNo = _this.data.phoneNo, 
      address = _this.data.address,
      region = _this.data.region;

    // 判断是否有名字
    if (name == null || name == '') {
      util.alertMsg('提示', '请输入姓名', function () {
        return false;
      }, null, false);
      return false;
    }
    if (postalCode == null || postalCode == '') {
      util.alertMsg('提示', '请输入邮政编码', function () {
        return false;
      }, null, false);
      return false;
    }
    if (phoneNo == null || phoneNo == '') {
      util.alertMsg('提示', '请输入电话号码', function () {
        return false;
      }, null, false);
      return false;
    }
    var isPhone = false;
    util.testPhone(phoneNo, function (rs) {
      if (rs) {
        isPhone = true;
      } else {
        isPhone = false;
      }
    });
    if (!isPhone) {
      // 取消加载中
      wx.hideLoading();
      util.alertMsg('警告', '请输入正确的手机号码', function () {
        console.log('用户点击确定')
      }, function () {
        console.log('用户点击取消')
      });
      return;
    }
    if (region[0] == '请选择') {
      util.alertMsg('提示', '请选择地区', function () {
        return false;
      }, null, false);
      return false;
    }
    return true;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindRegionChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  // 去**页
  goNavTo: function (e) {
    app.goNavTo(e, _this.data.buttonClicked, _this);
  },
  // 返回上一页
  goBack: function (e) {
    app.goBack(e, _this.data.buttonClicked, _this);
  },
})